//
//  RECViewController.m
//  CarFace
//
//  Created by dlios on 15/3/23.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "RECViewController.h"
#import "ADScrollView.h"
#import "UpStairSliderView.h"
#import "Newslist.h"
#import "RECViewCell.h"
#import "RECWebViewController.h"
#import "RecPhotosController.h"
#import "RECSearchViewController.h"
#import "RECFastNewsController.h"
//快报页面重用池声明
#import "RECFastCollectionViewCell.h"


//#warning 签订协议
@interface RECViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UpStairSliderDelegate, UIScrollViewDelegate, RECViewCellDelegate>
@property(nonatomic, retain)UICollectionView *colletionView;
@property(nonatomic, retain)ADScrollView *adVC;
@property(nonatomic, retain)UpStairSliderView *upStair;
@property(nonatomic, retain)NSMutableArray *upArr;

@property(nonatomic, retain)UITableView *tableOne;

@property(nonatomic, retain)NSMutableArray *listArr;


@property(nonatomic, retain)NSArray *addArr;  //地址栏

//传输首页是否刷新
@property(nonatomic, assign)BOOL isloadmore;

@end

@implementation RECViewController

- (void)dealloc
{
    [_listArr release];
    [_tableOne release];
    [_upArr release];
    [_upStair release];
    [_adVC release];
    [_colletionView release];
    [super dealloc];
}
- (void)handleData
{

    //广告
//    NSString *str = @"http://ad.app.autohome.com.cn/autov4.6/ad/FocusAdAndNewsListAd.ashx?newstype=0&pageindex=1&networkid=0&idfa=0&deviceid=352558065636350&mac=F0%3A25%3AB7%3AE2%3A30%3AE1&appid=2&platform=2&version=4.6.1&random=STulyz47U033L&a=2&pm=2&v=4.6.1%20HTTP/1.1";
    

    //移动广告栏
    NSString *str = @"http://app.api.autohome.com.cn/autov4.6/news/newslist-a2-pm2-v4.6.1-c0-nt0-p1-s20-l0.html";
    
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path = [NSString stringWithFormat:@"%@/%ld.aa", docPath, [str hash]];
 

    // html转化为json
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSLog(@"成功%@", responseObject);
        NSError *error = nil;
        NSJSONSerialization *result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
//        NSLog(@"%@", result);
        // 这里使用一个NSObject的方法-(NSUInteger)hash;获取一个字符串的哈希值, 当两个字符串完全一致的时候, 返回的哈希值是完全一样的, 每当有一点不同哈希值就完全不同.
        // 哈希值和MD5一样, 过程是不可逆的, 但是都可以产生同样的字符串返回的值是一样的结果.
        NSString *path = [NSString stringWithFormat:@"%@/%ld.aa", docPath, [str hash]];
        BOOL resultss = [NSKeyedArchiver archiveRootObject:responseObject toFile:path];
        NSLog(@"存储成功: %d", resultss);
        
        
        
        NSMutableArray *folArr = [[result valueForKey:@"result"] objectForKey:@"focusimg"];
        NSMutableArray *imageArr = [NSMutableArray array];
        
        for (NSMutableDictionary *dic in folArr) {
            NSString *str = [dic valueForKey:@"imgurl"];
            [imageArr addObject:str];
        }
//        NSLog(@"%@", imageArr);
        self.adVC.images = imageArr;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"失败%@", error);
        
    
    }];
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [self.colletionView reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor clearColor];
//    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBarHidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    //地址数组初始化
    self.addArr = [NSArray arrayWithObjects:
                   @"http://app.api.autohome.com.cn/autov4.6/news/newslist-a2-pm2-v4.6.1-c0-nt0-p1-s20-l0.html",
                   @"http://app.api.autohome.com.cn/autov4.6/news/fastnewslist-a2-pm2-v4.6.1-b0-l0-s20-lastid0.html",
                   @"http://app.api.autohome.com.cn/autov4.6/news/videolist-a2-pm2-v4.6.1-vt0-s20-lastid0.html",
                   @"http://app.api.autohome.com.cn/autov4.6/news/newslist-a2-pm2-v4.6.1-c0-nt1-p1-s20-l0.html",
                   @"http://app.api.autohome.com.cn/autov4.6/news/newslist-a2-pm2-v4.6.1-c0-nt3-p1-s20-l0.html",
                   @"http://app.api.autohome.com.cn/autov4.6/news/newslist-a2-pm2-v4.6.1-c0-nt60-p1-s20-l0.html",
                   @"http://app.api.autohome.com.cn/autov4.6/news/newslist-a2-pm2-v4.6.1-c210200-nt2-p1-s20-l0.html",
                   @"http://app.api.autohome.com.cn/autov4.6/news/newslist-a2-pm2-v4.6.1-c0-nt82-p1-s20-l0.html",
                   @"http://app.api.autohome.com.cn/autov4.6/news/newslist-a2-pm2-v4.6.1-c0-nt102-p1-s20-l0.html",
                   @"http://app.api.autohome.com.cn/autov4.6/news/newslist-a2-pm2-v4.6.1-c0-nt97-p1-s20-l0.html",
                   @"http://app.api.autohome.com.cn/autov4.6/news/newslist-a2-pm2-v4.6.1-c0-nt107-p1-s20-l0.html",
                   @"http://app.api.autohome.com.cn/autov4.6/news/newslist-a2-pm2-v4.6.1-c0-nt100-p1-s20-l0.html",
                   @"http://app.api.autohome.com.cn/autov4.6/news/videolist-a2-pm2-v4.6.1-vt8-s20-lastid0.html",
                   @"http://app.api.autohome.com.cn/autov4.6/news/shuokelist-a2-pm2-v4.6.1-s20-lastid0.html",nil];
    
    [self handleData];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.colletionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
    self.colletionView.backgroundColor = [UIColor clearColor];
    self.colletionView.delegate = self;
    self.colletionView.dataSource = self;
    [self.view addSubview:self.colletionView];
    self.colletionView.pagingEnabled = YES;
    self.colletionView.bounces = NO;
    [flowLayout release];
    [_colletionView release];
    
#pragma mark -- CollectionView重用池的重用标识
    //大部分页面
    [self.colletionView registerClass:[RECViewCell class] forCellWithReuseIdentifier:@"Recommand"];
    //快报页面独立重用池
//    [self.colletionView registerClass:[RECFastCollectionViewCell class] forCellWithReuseIdentifier:@"FastRec"];
    
    

    self.adVC = [[ADScrollView alloc] initWithFrame:CGRectMake(0, 32, self.view.frame.size.width, self.colletionView.frame.size.height / 4) ];
    [self.colletionView addSubview:self.adVC];
    [_adVC release];

    self.upArr = [NSMutableArray arrayWithObjects:@"最新", @"快报", @"视频", @"新闻",@"评测", @"导购", @"行情", @"用车", @"技术", @"文化", @"改装", @"邮寄", @"原创视频", @"说客", nil];
    
    self.upStair = [UpStairSliderView upStairWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 44) arr:_upArr image:[UIImage imageNamed:@"sousuo.png"] action:@selector(upAction:) target:self];
    self.upStair.delegate = self;
    self.upStair.rec = 99;
    [self.view addSubview:self.upStair];
    
}

//- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    NSLog(@"%ld", indexPath.item);
//    _upStair.blackLook = (NSIndexPath *)indexPath.item;
//    
//}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
//    NSLog(@"%ld", indexPath.item);
    _upStair.blackLook = [NSIndexPath indexPathForItem:indexPath.row inSection:0];
}
// button点击出现搜索页面
- (void)upAction:(UIButton *)button
{
    RECSearchViewController *search = [[RECSearchViewController alloc] init];
    [self presentViewController:search animated:YES completion:^{
        
    }];
    NSLog(@"1");
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
//    NSLog(@"---------------%ld", _upArr.count);
    return self.upArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//    NSLog(@"%ld", indexPath.item);
    
//    if (indexPath.item == 1) {
//        RECViewCell *cellFast = [collectionView dequeueReusableCellWithReuseIdentifier:@"FastRec" forIndexPath:indexPath];
//        return cellFast;
//    }
    
    RECViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Recommand" forIndexPath:indexPath];
    
    //设置cell为代理人,推出快报页面100489
    cell.delegate = self;
    NSDictionary *dic = @{@"num":[NSString stringWithFormat:@"%ld", indexPath.item],@"addStr":[NSString stringWithFormat:@"%@", self.addArr[indexPath.item]]};
    cell.addDic = dic;

    // 首页细胞样式不等于6的block, 触发media = 6时候, 调用此block
    cell.pushWebblock = ^(NSString *idStr, NSInteger item){
        RECWebViewController *webVC = [[RECWebViewController alloc] init];
        webVC.idStr = idStr;
        [self presentViewController:webVC animated:YES completion:^{
        }];
        [webVC release];
    };
    
    //首页等于6的遥控器
    cell.photoViewblock = ^(NSString *idStr){
        NSLog(@"%@", idStr);
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        RecPhotosController *phoVC = [[RecPhotosController alloc] initWithCollectionViewLayout:flowLayout];
        phoVC.idStr = idStr;
        [self presentViewController:phoVC animated:YES completion:^{
        
        }];
        [phoVC release];
        [flowLayout release];
        
    };
//    __block RECViewController *vc = self;
//    cell.fastNewsblock = ^(NSString *idStr){
//        RECFastNewsController *fastVC = [[RECFastNewsController alloc] init];
//        fastVC.idStr = idStr;
//        [vc presentViewController:fastVC animated:YES completion:^{
//            
//        }];
//        [fastVC release];
//    };
    
    return cell;

}

// 代理推出快报页面
- (void)pushPage:(NSString *)idStr
{
//    NSLog(@"----%@",idStr);
    RECFastNewsController *fastVC = [[RECFastNewsController alloc] init];
    fastVC.idStr = idStr;
    [self presentViewController:fastVC animated:YES completion:^{
        
    }];
    [fastVC release];
}

- (void)changeCollection:(NSInteger)changeNum
{
    [_colletionView setContentOffset:CGPointMake(changeNum * self.view.frame.size.width, 0) animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
